package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.dao.OrderMapper;
import com.qingcheng.pojo.order.Order;
import com.qingcheng.service.order.OrderService;
import com.qingcheng.service.order.WxPayService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;

public class MessageConsumer implements MessageListener {
    @Autowired
    private OrderService orderService;
    @Autowired
    private WxPayService wxPayService;
    @Reference
    private Order order;
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public void onMessage(Message message) {
        //取出订单号
        String orderId = new String(message.getBody());
        //先查询业务系统的订单状态，如果订单状态为未支付，调用微信支付查询订单的方法查询。
        Order order = orderService.findById(orderId);
        String payStatus = order.getPayStatus();
        Boolean isPay=false;
        if(!"0".equals(payStatus)) {
            isPay = wxPayService.findNative(orderId);
            // 如果返回结果是未支付，调用关闭订单的业务逻辑方法。
            if (isPay == false) {
                wxPayService.closeNative(orderId);
            }else {
                // 如果返回结果是已支付，实现补偿操作
                order.setPayStatus("1");
                orderMapper.updateByPrimaryKey(order);
            }
        }


    }
}
